package cn.chenhongchao.zhaobiaopingtai.controller;


import cn.chenhongchao.zhaobiaopingtai.common.api.CommonResult;
import cn.chenhongchao.zhaobiaopingtai.entity.Users;
import cn.chenhongchao.zhaobiaopingtai.service.impl.UsersServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhaoxinguo on 2018/06/05.
 */
@RestController
@RequestMapping("/users")
@Api(value = "注册管理", description = "注册管理")
public class RegisterController  {

    @Resource
    private UsersServiceImpl usersService;

    @Resource
    protected BCryptPasswordEncoder bCryptPasswordEncoder;
    @Resource
    private PasswordEncoder passwordEncoder;


    /**
     * 注册用户 默认开启白名单
     * @param
     */
    @ApiOperation(value = "注册用户")
    @PostMapping("/signup")
    public CommonResult signup(@RequestBody Users user) {
        if(usersService.getByUserName(user.getUsername()).size() != 0){
            return CommonResult.failed("用户已经存在");
        }
        /*user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword()).getBytes()));*/
        user.setPasswd(passwordEncoder.encode(user.getPasswd()));
//        LoginController loginController = new LoginController();
//        loginController.login("string",null,null);
        boolean register = usersService.register(user);
        if(register){
            return CommonResult.success("注册成功");
        }
        return  CommonResult.failed("注册失败");
    }

}
